package com.cloxtec.scada.utils;

import com.ramostear.captcha.core.Captcha;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

public class MyCaptcha extends Captcha {

    @Override
    public void drawImage(char[] chars, OutputStream output) {
        BufferedImage img = new BufferedImage(this.width, this.height, 4);
        Graphics2D g = (Graphics2D)img.getGraphics();
        g.setBackground(Color.GRAY);
        g.fillRect(0, 0, this.width, this.height);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        this.drawLine(g);
        this.drawOval(g);
        this.drawBezierLine(g);
        g.setFont(this.getFont());
        FontMetrics fontMetrics = g.getFontMetrics();
        int fw = this.width / chars.length - 2;
        int fm = (fw - (int)fontMetrics.getStringBounds("8", g).getWidth()) / 2;

        for(int i = 0; i < chars.length; ++i) {
            g.setColor(new Color(0, 0, 255));
            int fh = this.height - (this.height - (int)fontMetrics.getStringBounds(String.valueOf(chars[i]), g).getHeight() >> 1);
            g.drawString(String.valueOf(chars[i]), i * fw + fm, fh);
        }

        g.dispose();

        try {
            ImageIO.write(img, "png", output);
        } catch (IOException var21) {
            var21.printStackTrace();
        }

        try {
            output.flush();
        } catch (IOException var19) {
            var19.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException var18) {
                var18.printStackTrace();
            }

        }
    }
}
